
from functools import cache

class Solution:

    def maxUncrossedLines(self, nums1, nums2):
        m, n = len(nums1), len(nums2)

        @cache
        def dfs(i, j):
            if i < 0 or j < 0:
                return 0
            
            if nums1[i] == nums2[j]:

                return dfs(i - 1, j - 1) + 1
            
            return max(dfs(i - 1, j), dfs(i, j - 1))
        
        return(m - 1, n - 1)
    
